package com.zheng.PointTest.controller;

import com.alibaba.fastjson.JSON;
import com.zheng.PointTest.base.CommonConstants;
import com.zheng.PointTest.base.response.BaseException;
import com.zheng.PointTest.base.response.BaseResponse;
import com.zheng.PointTest.base.response.ResponseCode;
import com.zheng.PointTest.base.response.ResponseResult;
import com.zheng.PointTest.entity.*;
import com.zheng.PointTest.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * className:UserController
 * Package:com.zheng.PointTest.controller
 * Description:
 *
 * @Author:ZhengJinLong Create:2024/6/30 - 23:57
 * @Version:v1.0
 */
@BaseResponse
@RestController
@RequestMapping("/users")
public class UserController{

    /**
     * 查询所有用户信息
     */
    @Resource
    UserMapper userMapper;

    @GetMapping("/all")
    public List<userInfo> queryAllUser(){
        List<userInfo> users = userMapper.queryAllUser();
        if (users.isEmpty()) {
            throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
        }
        return users;
    }

    @PostMapping("/register")
    public ResponseResult registerUserByInfo(@RequestBody String registerJson){
        System.out.println("login："+registerJson);
        //{"password":"admin","name":"admin"}
        Map map = JSON.parseObject(registerJson, Map.class);
        //判断用户是否存在
        String name = map.get("userName").toString();
        String password = map.get("password").toString();
        userInfo user = userMapper.queryUserByName(name);
        if (user != null) {
            return new ResponseResult(CommonConstants.RESPONSECODE_FAIL,"抱歉，用户名已存在",null);
        }

        //注册用户
        userMapper.registerUserByInfo(new userInfo("avatar",name,password));

        //通过查询，判断该用户是否注册成功
        userInfo user2 = userMapper.queryUserByName(name);
        if (user2 != null) {
            return new ResponseResult(CommonConstants.RESPONSECODE_SUCCESS,"恭喜!!,注册成功",null);
        }
        return new ResponseResult(CommonConstants.RESPONSECODE_FAIL,"抱歉，注册失败，请重试",null);
    }

    @PostMapping("/login")
    public ResponseResult login(@RequestBody String loginJson){
        System.out.println("login："+loginJson);
        //{"password":"admin","name":"admin"}
        Map map = JSON.parseObject(loginJson, Map.class);
        //判断用户是否存在
        String name = map.get("userName").toString();
        String password = map.get("password").toString();
        userInfo user = userMapper.queryUserByName(name);
        if (user == null) {

            return new ResponseResult(ResponseCode.RESOURCES_NOT_EXIST.getCode(), "用户名不存在", "");

        }
        //判断用户密码是否正确
        if (!user.getPassword().equals(password)) {
            return new ResponseResult(ResponseCode.RESOURCES_NOT_EXIST.getCode(), "密码错误", "");

        }
        LoginResponseType responseType = new LoginResponseType("",user);
        return new ResponseResult(ResponseCode.SUCCESS.getCode(), "恭喜"+name+"登录成功", responseType);
    }

}
